import {
    getMessage,
    hasRead,
    removeReaded,
    restoreTrash
  } from '../../api/api'
  
  export default {
    state: {
      hasGetInfo: false,
      unreadCount: 0,
      // messageUnreadList: [],
      // messageReadedList: [],
      messageTrashList: [],
      applicationList: [],
      answerNotificationList: [],
      userFeedbackList: [],
      systemList: [],
      applicationUnreadCount: 0,
      answerNotificationUnreadCount: 0,
      userFeedbackUnreadCount: 0,
      systemUnreadCount: 0,
      messageContentStore: {}
    },
    mutations: {
      setMessageCount (state, count) {
        state.unreadCount = count
      },
      setApplicationUnreadCount (state, count) {
        state.applicationUnreadCount = count
      },
      setAnswerNotificationUnreadCount (state, count) {
        state.answerNotificationUnreadCount = count
      },
      setUserFeedbackUnreadCount (state, count) {
        state.userFeedbackUnreadCount = count
      },
      setSystemUnreadCount (state, count) {
        state.systemUnreadCount = count
      },
      

      // setMessageUnreadList (state, list) {
      //   state.messageUnreadList = list
      // },
      // setMessageReadedList (state, list) {
      //   state.messageReadedList = list
      // },
      setMessageTrashList (state, list) {
        state.messageTrashList = list
      },
      setApplicationList (state, list) {
        state.applicationList = list
      },
      setAnswerNotificationList (state, list) {
        state.answerNotificationList = list
      },
      setUserFeedbackList (state, list) {
        state.userFeedbackList = list
      },
      setSystemList (state, list) {
        state.systemList = list
      },
      updateMessageContentStore (state, { msg_id, content }) {
        state.messageContentStore[msg_id] = content
      },
      moveMsg (state, { from, to, msg_id }) {
        const index = state[from].findIndex(_ => _._id === msg_id)
        let msgItem = state[from].splice(index, 1)[0]
        if(to==='messageTrashList'){
          msgItem = {...msgItem, status: 'trash'}
        } else {
          msgItem = {...msgItem, status: 'readed'}
        }
        state[to].unshift(msgItem)
      }
    },
    getters: {
      // messageUnreadCount: state => state.messageUnreadList.length,
      // messageReadedCount: state => state.messageReadedList.length,
      // messageTrashCount: state => state.messageTrashList.length
      applicationUnreadCount: state => state.applicationUnreadCount,
      answerNotificationUnreadCount: state => state.answerNotificationUnreadCount,
      userFeedbackUnreadCount: state => state.userFeedbackUnreadCount,
      systemUnreadCount: state => state.systemUnreadCount,
      messageUnreadCount: state => state.unreadCount 
    },
    actions: {
      // 获取消息列表，其中包含未读、已读、回收站三个列表
      getMessageList ({ state, commit },uuid) {
        return new Promise((resolve, reject) => {
            // console.log(uuid)
          getMessage({UUID: uuid}).then(res => {
              // console.log(res)
            const { application, answer_notification, user_feedback, system, trash } = res.results
            // console.log(application.list)
            commit('setApplicationList', application.list)
            commit('setAnswerNotificationList', answer_notification.list)
            commit('setUserFeedbackList', user_feedback.list)
            commit('setSystemList', system.list)
            commit('setMessageTrashList', trash)
            commit('setApplicationUnreadCount', application.unread)
            commit('setAnswerNotificationUnreadCount', answer_notification.unread)
            commit('setUserFeedbackUnreadCount', user_feedback.unread)
            commit('setSystemUnreadCount', system.unread)
            commit('setMessageCount', application.unread+answer_notification.unread+user_feedback.unread+system.unread)
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 把一个未读消息标记为已读
      hasRead ({ state, commit }, { msg_id, type }) {
        return new Promise((resolve, reject) => {
          hasRead({_id: msg_id}).then((res) => {
            // commit('moveMsg', {
            //   from: 'messageUnreadList',
            //   to: 'messageReadedList',
            //   msg_id
            // })
            switch(type){
              case 'application': 
                commit('setApplicationUnreadCount', state.applicationUnreadCount-1)
                commit('setMessageCount', state.unreadCount-1)
                break
              case 'answer_notification': 
                commit('setAnswerNotificationUnreadCount', state.answerNotificationUnreadCount-1)
                commit('setMessageCount', state.unreadCount-1)
                break
              case 'user_feedback': 
                commit('setUserFeedbackUnreadCount', state.userFeedbackUnreadCount-1)
                commit('setMessageCount', state.unreadCount-1)
                break
              case 'system': 
                commit('setSystemUnreadCount', state.systemUnreadCount-1)
                commit('setMessageCount', state.unreadCount-1)
                break
              default:
                break
            }
            // commit('setMessageCount', state.unreadCount - 1)
            resolve(res)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 删除一个已读消息到回收站
      removeReaded ({ commit }, { msg_id, type }) {
        return new Promise((resolve, reject) => {
          removeReaded({_id:  msg_id}).then(() => {
            switch(type){
              case 'application': 
                commit('moveMsg', {
                  from: 'applicationList',
                  to: 'messageTrashList',
                  msg_id
                })
                break
              case 'answer_notification': 
                commit('moveMsg', {
                  from: 'answerNotificationList',
                  to: 'messageTrashList',
                  msg_id
                })
                break
              case 'user_feedback': 
                commit('moveMsg', {
                  from: 'userFeedbackList',
                  to: 'messageTrashList',
                  msg_id
                })
                break
              case 'system': 
                commit('moveMsg', {
                  from: 'systemList',
                  to: 'messageTrashList',
                  msg_id
                })
                break
              default:
                break
            }
            resolve(1)
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 还原一个已删除消息到已读消息
      restoreTrash ({ commit }, { msg_id, type }) {
        return new Promise((resolve, reject) => {
          restoreTrash({_id: msg_id}).then(() => {
            switch(type){
              case 'application': 
                commit('moveMsg', {
                  from: 'messageTrashList',
                  to: 'applicationList',
                  msg_id
                })
                break
              case 'answer_notification': 
                commit('moveMsg', {
                  from: 'messageTrashList',
                  to: 'answerNotificationList',
                  msg_id
                })
                break
              case 'user_feedback': 
                commit('moveMsg', {
                  from: 'messageTrashList',
                  to: 'userFeedbackList',
                  msg_id
                })
                break
              case 'system': 
                commit('moveMsg', {
                  from: 'messageTrashList',
                  to: 'systemList',
                  msg_id
                })
                break
              default:
                break
            }
            resolve(1)
          }).catch(error => {
            reject(error)
          })
        })
      }
    }
  }
  